package com.ld.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.ld.dao.ClassesDao;
import com.ld.dao.StudentDao;
import com.ld.pojo.Classes;
import com.ld.pojo.PageResult;
import com.ld.pojo.Student;
import com.ld.pojo.StudentVo;
import com.ld.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * @Author: Liudao
 * @Date: 2022-08-26-16:34
 * @Description:
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassesDao classesDao;

    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
        //静态SQL查询的两种方法：
        //方法一：根据方法名生成sql语句
        // Page<Student> studentPage = studentDao.findBySnameLikeAndAddrLike(vo.getSname(), vo.getAddr(), PageRequest.of(page - 1, pagesize));
        //方法二：
        // Page<Student> studentPage = studentDao.search(vo.getSname(), vo.getAddr(),PageRequest.of(page-1,pagesize));

        //动态SQL查询的方法：
        Specification<Student> specification = new Specification<Student>() {
            //1. 定义查询条件的集合
            List<Predicate> predicates = new ArrayList<>();
            @Override
            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //2. 添加各种条件到上面的条件集合中
                if(StrUtil.isNotBlank(vo.getSname())){
                    predicates.add(cb.like(root.get("sname").as(String.class),"%" + vo.getSname() + "%"));
                }
                if(StrUtil.isNotBlank(vo.getAddr())){
                    predicates.add(cb.like(root.get("addr").as(String.class),"%" + vo.getAddr() + "%"));
                }
                if(vo.getCid() != 0){
                    predicates.add(cb.equal(root.get("cid").as(int.class),vo.getCid()));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        //2 根据条件进行分页查询
        Page<Student> studentPage = studentDao.findAll(specification, PageRequest.of(page-1, pagesize));
        //3 关联学生和班级
        for (Student student : studentPage.getContent()) {
            Optional<Classes> optionalClasses = classesDao.findById(student.getCid());
            Classes classes = optionalClasses.get();
            String cname = classes.getCname();
            student.setCname(cname);
        }
        //4 返回
        return new PageResult<>(studentPage.getTotalElements(),studentPage.getTotalPages(),studentPage.getContent());
    }

    public void add(Student student) {
        studentDao.save(student);
    }

    public void update(Student student) {
        studentDao.save(student);
    }

    public void delele(int sid) {
        studentDao.deleteById(sid);
    }
}
